    
import re

class LogAnalysis:

    def __init__(self) -> None:
        
        self.all_sensor_info_list = self.get_sensor_info_by_log

    def get_sensor_info_by_log(self):

        regex = r"{'codr': '4/5', 'data': '.*?', 'datetime': '(.*?)', 'datr': '.*?', 'devaddr': '(.*?)', .*?, 'text': '(.*?)'}"

        with open("/var/log/receive_ns_task.log", encoding="utf-8") as f:
            log_content = f.read()
            result = re.findall(regex, log_content)
            

        return result
    
    def get_sensor_info_last(self, sensor_mac):

        '''return ('2023-10-28T01:19:55Z', '9A8BE4C6', '058A0B000E00040011EF73')'''

        sensor_info = self.all_sensor_info_list()

        for i in range(len(sensor_info)-1, -1, -1):
            
            if sensor_info[i][1] == sensor_mac:


                return sensor_info[i]
            
        else:

            return None
    